import time
from tqdm import tqdm
import selenium
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait


def get_data():
    has_flag = 0
    top_three_country = []

    # 获取品类名称
    class_name = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located(
            (By.XPATH, '// *[ @ id = "app"] / div[2] / div[2] / div / div[2] / div[1] / div[1]'))).text[:-3]

    # 前三名的品牌名、国家
    inputEmailElement_1 = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, '//*[@id="app"]/div[2]/div[3]/div/div[2]/div/div[2]/div[1]/div[1]')))
    if '中国' in inputEmailElement_1.text:
        has_flag = 1
    top_three_country.append({inputEmailElement_1.text.split('\n')[2]: inputEmailElement_1.text.split('\n')[1]})

    inputEmailElement_2 = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, '//*[@id="app"]/div[2]/div[3]/div/div[2]/div/div[2]/div[2]/div[1]')))
    if '中国' in inputEmailElement_2.text:
        has_flag = 1
    top_three_country.append({inputEmailElement_2.text.split('\n')[2]: inputEmailElement_2.text.split('\n')[1]})

    inputEmailElement_3 = WebDriverWait(driver, 10).until(
        EC.presence_of_element_located((By.XPATH, '//*[@id="app"]/div[2]/div[3]/div/div[2]/div/div[2]/div[3]/div[1]')))
    if '中国' in inputEmailElement_3.text:
        has_flag = 1
    top_three_country.append({inputEmailElement_3.text.split('\n')[2]: inputEmailElement_3.text.split('\n')[1]})
    return class_name, top_three_country, has_flag


# 创建火狐窗口
driver = webdriver.Firefox()

# 打开盖得排行首页
driver.get('https://www.guiderank-app.com/')

# 接收所有数据的字典
all_has_china_class_dict = {}
f = open("all_has_china_class_dict.txt", 'w', encoding='utf-8')

# 对26个类目循环点击
for index in tqdm(range(2, 27)):
    WebDriverWait(driver, 10).until(EC.presence_of_element_located(
        (By.XPATH, '//*[@id="app"]/div[2]/div[2]/div[2]/div[2]/div[1]/div/div[{}]'.format(index)))).click()
    for index_2 in range(1, 11):
        time.sleep(1)
        for index_3 in range(1, 100):
            # print('index2:{}'.format(index_2))
            # print('index3:{}'.format(index_3))
            try:
                ele = WebDriverWait(driver, 10).until(EC.presence_of_element_located(
                    (
                    By.XPATH, '//*[@id="app"]/div[2]/div[2]/div[2]/div[2]/div[2]/div/div/div[{}]/div[2]/div[{}]'.format(
                        index_2, index_3))))
                ele.click()
                a = driver.find_element_by_xpath('//*[@id="app"]/div[2]/div[3]/div/div[1]/div[1]/div')
                time.sleep(1)
                class_name, top_three_country, has_flag = get_data()
                if has_flag == 1:
                    all_has_china_class_dict[class_name] = top_three_country
                    print('class_name:{}'.format(class_name))
                    print('top_three_country:{}'.format(top_three_country))
                    print('\n')
                driver.back()

            except selenium.common.exceptions.NoSuchElementException:
                time.sleep(1)
                index_2 += 1
                driver.back()
                break
            except selenium.common.exceptions.TimeoutException:
                try:
                    time.sleep(1)
                    index_2 += 1
                    driver.back()
                    break
                except selenium.common.exceptions.TimeoutException:
                    time.sleep(1)
                    break
                except:
                    print('跳出')
                    break
            except:
                print('跳出')
                break

print('执行完成！')
# save to local
f.write(str(all_has_china_class_dict))
f.close()

